*use "C:\Users\alexgkuo\Dropbox\Spain Economic Ministry Grant 2017 completion\data\tc_data.dta"


use "C:\Users\Alex\Dropbox\Spain Economic Ministry Grant 2017 completion\data\tc_data.dta"


*Formatting raw data

*KEEP all variables that are needed

keep responseid country workers compensate training  stop edu6c rti risk01 routine01 routine female age edu6c employment income7c ocupation 

*Label and coding of dependent variables
label variable compensate "Compensate laid-off workers with subsidies"

recode compensate 1/3=0 4/5=1, gen(support_compensate)
label var support_compensate "support compensate, binary"
tab support_compensate
rename support_compensate compensate_bi

recode training 1/4=0 5=1, gen(ssupport_training)
label var ssupport_training "strongly support training"
tab ssupport_training 

recode training 1/3=0 4/5=1, gen(support_training)
label var support_training "support training, binary"
tab support_training 
rename support_training training_bi

recode stop 1/3=0 4/5=1, gen(stop_bi)
label var stop_bi "support preventing firm firm"
tab stop_bi

*Relabeling treatment categories for firm closing vignettes

gen por_worker=0
replace por_worker=1 if country==1 & workers==2
move por_worker country
tab por_worker

gen por_robot=0
replace por_robot=1 if country==1 & workers==1
move por_robot country
tab por_robot

gen spa_worker=0
replace spa_worker=1 if country==3 & workers==2
move spa_worker country
tab spa_worker

gen spa_robot=0
replace spa_robot=1 if country==3 & workers==1
move spa_robot country
tab spa_robot

gen chi_worker=0
replace chi_worker=1 if country==2 & workers==2
move chi_worker country
tab chi_worker

gen chi_robot=0
replace chi_robot=1 if country==2 & workers==1
move chi_robot country
tab chi_robot

*Generate numeric treatment assignment variables

gen firm_treat=.
replace firm_treat=1 if spa_worker==1
replace firm_treat=2 if spa_robot==1
replace firm_treat=3 if por_worker==1
replace firm_treat=4 if por_robot==1
replace firm_treat=5 if chi_worker==1
replace firm_treat=6 if chi_robot==1
tab firm_treat

label define firm_treat 1 "spa-wkr" 2 "spa-rbt" 3 "por-wkr" 4 "por-rbt" 5 "chi-wkr" 6 "chi-rbt" , replace
label values firm_treat firm_treat
tab firm_treat

*Rescaling policy preferences 0-1 (for linear models)
	
gen compensate01=compensate
replace compensate01=(compensate-1)/4 
tab compensate01
label var compensate01 "compensate 0-1"

gen training01=training
replace training01=(training-1)/4 
tab training01
label var training01 "training 0-1"

gen stop01=stop
replace stop01=(stop-1)/4 
tab stop01
label var stop01 "stop 0-1"

*Binary measure of education
recode edu6c 1/4=0 5/6=1, gen(edu2c)
label var edu2c "high v low educ"
label define edu2c 0 "low educ" 1 "high educ" , replace
label values edu2c edu2c
tab edu2c

*Coding some univerristy or more as high education
recode edu6c 1/4=0 5/6=1, gen(edu_hi)
move edu_hi edu6c
label var edu_hi "high education"
tab edu_hi

*Generate RTI  scaled 0-1

gen rti_01=rti
replace rti_01=(rti_01+1.92)/3.33 
tab rti_01
label var rti_01 "RTI scaled 0-1"
gen rti01=rti_01


*Generate categorical variables of automation risk
*Quartiles of 3 main indicators of risk

xtile rti01_quartile=rti01,n(4)

rename rti01_quartile rti_quartile
label var rti_quartile "quartiles of RTI"

xtile risk_quartile=risk,n(4)
label var risk_quartile "quartiles of task risk"

xtile routine_quartile=routine,n(4)
label var routine_quartile "quartiles of job routine"
tab routine_quartile


*Generate a high versus low RTI variable
xtile rti_hi =rti01,n(2)
tab rti_hi
recode rti_hi 1=0 2=1
label var rti_hi "RTI hi"

*Generate a high versus low risk/TLRA variable
xtile risk_hi =risk01,n(2)
tab risk_hi
recode risk_hi 1=0 2=1
label var risk_hi "Task risk hi"

*Generate a binary variable for all robot treatments and for all openness treatments, other treatments
recode workers 2=0, gen(robots_treat)
tab robots_treat
label var robots_treat "robots treatment, all countries"

generate offshore_treat=0
replace offshore_treat=1 if (por_worker==1 | chi_worker==1)
tab offshore_treat
label var offshore_treat "offshoring workers treat"

generate anyforeign_treat=0
replace anyforeign_treat=1 if (por_worker==1 | chi_worker==1 | por_robot==1 | chi_robot==1)
tab anyforeign_treat
label var anyforeign_treat "any foreign country"

*End variable recoding

*FIGURE 1 (BW): Mean policy support by treatment condition
graph bar (mean) compensate_bi training_bi stop_bi, over(firm_treat) scheme(s2mono) ytitle(Proportion favoring policy) legend(order(1 "support compensation" 2 "support training" 3 "support prevention"))

*Table 1: Ordered logistic regressions, policy support

ologit compensate spa_robot por_worker por_robot chi_worker chi_robot 
est sto M1

ologit training spa_robot por_worker por_robot chi_worker chi_robot 
est sto M2

ologit stop spa_robot por_worker por_robot chi_worker chi_robot 
est sto M3

ologit compensate spa_robot por_worker por_robot chi_worker chi_robot i.rti_quartile i.female i.age i.edu_hi i.employment i.income7c  i.ocupation
est sto M4

ologit training spa_robot por_worker por_robot chi_worker chi_robot i.rti_quartile i.female i.age i.edu_hi i.employment i.income7c  i.ocupation
est sto M5

ologit stop spa_robot por_worker por_robot chi_worker chi_robot i.rti_quartile i.female i.age i.edu_hi i.employment i.income7c  i.ocupation
est sto M6

esttab M1 M2 M3 M4 M5 M6 using "firm closing policies_ologit.rtf" , replace b(a2) se(a2) r2 se star(* 0.10 ** 0.05 *** 0.01) 

*Table C1: Use of continuous variables for demographic variables

ologit compensate spa_robot por_worker por_robot chi_worker chi_robot rti01 female age edu_hi employment income7c  i.ocupation
est sto M1

ologit training spa_robot por_worker por_robot chi_worker chi_robot rti01 female age edu_hi employment income7c  i.ocupation
est sto M2

ologit stop spa_robot por_worker por_robot chi_worker chi_robot rti01 female age edu_hi employment income7c  i.ocupation
est sto M3

esttab M1 M2 M3 using "firm closing policies_continuous_ologit.rtf" , replace b(a2) se(a2) r2 se star(* 0.10 ** 0.05 *** 0.01) 


*Table C2: Linear models with coding of dependent variables ranging 0-1 (not binary)

reg compensate01 spa_robot por_worker por_robot chi_worker chi_robot 
est sto M1

reg training01 spa_robot por_worker por_robot chi_worker chi_robot 
est sto M2

reg stop01 spa_robot por_worker por_robot chi_worker chi_robot 
est sto M3

reg compensate01 spa_robot por_worker por_robot chi_worker chi_robot i.rti_quartile i.female i.age i.edu6c i.employment i.income7c  i.ocupation
est sto M4

reg training01 spa_robot por_worker por_robot chi_worker chi_robot i.rti_quartile i.female i.age i.edu6c i.employment i.income7c  i.ocupation
est sto M5

reg stop01 spa_robot por_worker por_robot chi_worker chi_robot i.rti_quartile i.female i.age i.edu6c i.employment i.income7c  i.ocupation
est sto M6

esttab M1 M2 M3 M4 M5 M6 using "firm closing policies_ols.rtf" , replace b(a2) se(a2) r2 se star(* 0.10 ** 0.05 *** 0.01) 


*Table C3: Logistic models with binary coding of dependent variables
logit compensate_bi spa_robot por_worker por_robot chi_worker chi_robot 
est sto M1

logit training_bi spa_robot por_worker por_robot chi_worker chi_robot 
est sto M2

logit stop_bi spa_robot por_worker por_robot chi_worker chi_robot 
est sto M3

logit compensate_bi spa_robot por_worker por_robot chi_worker chi_robot i.rti_quartile i.female i.age i.edu_hi i.employment i.income7c  i.ocupation
est sto M4

logit training_bi spa_robot por_worker por_robot chi_worker chi_robot i.rti_quartile i.female i.age i.edu_hi i.employment i.income7c  i.ocupation
est sto M5

logit stop_bi spa_robot por_worker por_robot chi_worker chi_robot i.rti_quartile i.female i.age i.edu_hi i.employment i.income7c  i.ocupation
est sto M6

esttab M1 M2 M3 M4 M5 M6 using "firm closing policies_logit.rtf" , replace b(a2) se(a2) r2 se star(* 0.10 ** 0.05 *** 0.01) 


*Table C4: Main specification with alternative risk variable (TLRA)

ologit compensate spa_robot por_worker por_robot chi_worker chi_robot i.risk_quartile i.female i.age i.edu_hi i.employment i.income7c  i.ocupation
est sto M1

ologit training spa_robot por_worker por_robot chi_worker chi_robot i.risk_quartile i.female i.age i.edu_hi i.employment i.income7c  i.ocupation
est sto M2

ologit stop spa_robot por_worker por_robot chi_worker chi_robot i.risk_quartile i.female i.age i.edu_hi i.employment i.income7c  i.ocupation
est sto M3

esttab M1 M2 M3 using "firm closing policies_ologit_altrisk.rtf" , replace b(a2) se(a2) r2 se star(* 0.10 ** 0.05 *** 0.01) 

*Table D1: Conditional effects of RTI (binary measure) 

*Interactions with high vs low levels of RTI [ologit specification]

ologit compensate i.spa_robot##i.rti_hi i.por_worker##i.rti_hi i.por_robot##i.rti_hi i.chi_worker##i.rti_hi i.chi_robot##i.rti_hi 
est sto M1

ologit training i.spa_robot##i.rti_hi i.por_worker##i.rti_hi i.por_robot##i.rti_hi i.chi_worker##i.rti_hi i.chi_robot##i.rti_hi 
est sto M2

ologit stop i.spa_robot##i.rti_hi i.por_worker##i.rti_hi i.por_robot##i.rti_hi i.chi_worker##i.rti_hi i.chi_robot##i.rti_hi  
est sto M3

*Interactions with high vs low levels of RTI [OLS specification]
reg compensate01  i.spa_robot##i.rti_hi i.por_worker##i.rti_hi i.por_robot##i.rti_hi i.chi_worker##i.rti_hi i.chi_robot##i.rti_hi 
est sto M4

reg training01  i.spa_robot##i.rti_hi i.por_worker##i.rti_hi i.por_robot##i.rti_hi i.chi_worker##i.rti_hi i.chi_robot##i.rti_hi 
est sto M5

reg stop01  i.spa_robot##i.rti_hi i.por_worker##i.rti_hi i.por_robot##i.rti_hi i.chi_worker##i.rti_hi i.chi_robot##i.rti_hi  
est sto M6

esttab M1 M2 M3 M4 M5 M6 using "firm closing policies_RTIcond.rtf" , replace b(a2) se(a2) r2 se star(* 0.10 ** 0.05 *** 0.01) 


*Table D2: Interactions with high vs low levels of task risk [ologit specification]
ologit compensate i.spa_robot##i.risk_hi i.por_worker##i.risk_hi i.por_robot##i.risk_hi i.chi_worker##i.risk_hi i.chi_robot##i.risk_hi 
est sto M1

ologit training i.spa_robot##i.risk_hi i.por_worker##i.risk_hi i.por_robot##i.risk_hi i.chi_worker##i.risk_hi i.chi_robot##i.risk_hi 
est sto M2

ologit stop i.spa_robot##i.risk_hi i.por_worker##i.risk_hi i.por_robot##i.risk_hi i.chi_worker##i.risk_hi i.chi_robot##i.risk_hi  
est sto M3

*Interactions with high vs low levels of task risk [OLS specification]
reg compensate01  i.spa_robot##i.risk_hi i.por_worker##i.risk_hi i.por_robot##i.risk_hi i.chi_worker##i.risk_hi i.chi_robot##i.risk_hi 
est sto M4

reg training01  i.spa_robot##i.risk_hi i.por_worker##i.risk_hi i.por_robot##i.risk_hi i.chi_worker##i.risk_hi i.chi_robot##i.risk_hi 
est sto M5

reg stop01  i.spa_robot##i.risk_hi i.por_worker##i.risk_hi i.por_robot##i.risk_hi i.chi_worker##i.risk_hi i.chi_robot##i.risk_hi  
est sto M6

esttab M1 M2 M3 M4 M5 M6 using "firm closing policies_TLRAcond.rtf" , replace b(a2) se(a2) r2 se star(* 0.10 ** 0.05 *** 0.01) 

*Table E: Aggregrate treatment assignments

*Treatments: 3 robot scenarios + 2 offshoring, baseline = spa-worker
ologit compensate robots_treat offshore_treat
est sto M1

ologit training robots_treat offshore_treat
est sto M2

ologit stop robots_treat offshore_treat
est sto M3

*Treatments: mentioning any foreign country [robots or workers] + spanish robot, baseline= spanish workers 
ologit compensate anyforeign_treat spa_robot
est sto M4

ologit training anyforeign_treat spa_robot
est sto M5

ologit stop anyforeign_treat spa_robot
est sto M6

*Treatments: mentioning any foreign country [robots or workers], baseline = spanish workers | robots
ologit compensate anyforeign_treat 
est sto M7

ologit training anyforeign_treat 
est sto M8

ologit stop anyforeign_treat 
est sto M9

esttab M1 M2 M3 M4 M5 M6 M7 M8 M9 using "firms_lumped_treats_ologit.rtf" , replace b(a2) se(a2) r2 se star(* 0.10 ** 0.05 *** 0.01) 

*Table H1: Balance tests

logit spa_worker female age edu_hi employment income7c  ocupation rti_quartile 
est sto M1

logit spa_robot female age edu_hi employment income7c  ocupation rti_quartile
est sto M2

logit por_worker  female age edu_hi employment income7c  ocupation  rti_quartile 
est sto M3

logit por_robot  female age edu_hi employment income7c  ocupation  rti_quartile 
est sto M4

logit chi_worker female age edu_hi employment income7c  ocupation rti_quartile
est sto M5

logit chi_robot  female age edu_hi employment income7c  ocupation rti_quartile
est sto M6

esttab M1 M2 M3 M4 M5 M6 using "firm closing policies_balance.rtf" , replace b(a2) se(a2) r2 se star(* 0.05 ** 0.01 *** 0.001) 

